package cn.controller;

import cn.entity.Kind;
import cn.service.KindService;
import cn.util.ServerResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/kind")
public class KindController {
    @Autowired
    KindService kindService;
    //JSON
    @RequestMapping("list")
    public ServerResponse list(Integer page, Integer limit,String name){
        QueryWrapper<Kind> wrapper=new QueryWrapper<Kind>();
        if(!StringUtils.isNullOrEmpty(name)){
            wrapper.like("name",name);
        }
        wrapper.eq("isdel",0);
        IPage<Kind> page_list=new Page<Kind>(page,limit);
        page_list=kindService.page(page_list,wrapper);
        long count=page_list.getTotal();
        List<Kind> list=page_list.getRecords();
        return new ServerResponse("0","操作成功",count,list);
    }

    @RequestMapping("login")
    public ServerResponse login(String username, String password, String vcode,Integer role){
        // 获取验证码
        //String code = (String) session.getAttribute("code");
       // if (code != null && code.equalsIgnoreCase(vcode)) {
            QueryWrapper<Kind> wrapper=new QueryWrapper<Kind>();
            wrapper.eq("username",username);
            wrapper.eq("password",password);
            wrapper.eq("role",role);
            wrapper.eq("isdel",0);
            List<Kind> list=kindService.list(wrapper);
            if(list.size()>0){
                Kind kind=list.get(0);
                return new ServerResponse("0","登陆成功!",1L,kind);
            }else{
                return new ServerResponse("1","用户名或密码错误!");
            }
//        }else{
//            return  new ServerResponse("1", "验证码错误!");
//        }
    }

    @RequestMapping("add")
    public ServerResponse add(Kind kind){
        kind.setIsdel(0);
        boolean flag=kindService.save(kind);
        if(flag){
            return new ServerResponse("0","操作成功!");
        }else{
            return new ServerResponse("1","操作失败!");
        }
    }

    @RequestMapping("update")
    public ServerResponse update(Kind kind){
        boolean flag=kindService.updateById(kind);
        if(flag){
            return new ServerResponse("0","操作成功!");
        }else{
            return new ServerResponse("1","操作失败!");
        }
    }

    @RequestMapping("delete")
    public ServerResponse delete(Kind kind){
        kind.setIsdel(1);
        boolean flag=kindService.updateById(kind);
        if(flag){
            return new ServerResponse("0","操作成功!");
        }else{
            return new ServerResponse("1","操作失败!");
        }
    }

    @RequestMapping("jsonlist")
    public ServerResponse jsonlist(){
        QueryWrapper<Kind> wrapper=new QueryWrapper<Kind>();
        wrapper.eq("isdel",0);
        List<Kind> list=kindService.list(wrapper);
        return new ServerResponse("0","操作成功",0L,list);
    }
}
